package com.lt.base;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Paint.FontMetrics;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.Nullable;
import androidx.annotation.RequiresApi;

public class BaseView extends View {

    public final String TAG = this.getClass().getSimpleName();

    public BaseView(Context context) {
        super(context, null);
        // TODO Auto-generated constructor stub
    }

    public BaseView(Context context, AttributeSet attrs) {
        super(context, attrs, 0);
        // TODO Auto-generated constructor stub
    }

    public BaseView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        // TODO Auto-generated constructor stub
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public BaseView(Context context, @Nullable AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
    }

    protected int dip2px(float dip) {
        final float scale = getContext().getResources().getDisplayMetrics().density;
        return (int) (dip * scale + 0.5f);
    }

    public int sp2px(float spValue) {
        final float fontScale = getContext().getResources().getDisplayMetrics().scaledDensity;
        return (int) (spValue * fontScale + 0.5f);
    }

    /**
     * 文字绘制Y轴调整
     */
    protected int getFontY(float centerY, Paint p) {
        FontMetrics fontMetrics = p.getFontMetrics();
        return (int) (centerY - (fontMetrics.ascent + fontMetrics.descent) / 2);
    }

    /**
     * 获取文件宽度
     */
    protected float getTextLength(String str, Paint p) {
        return p.measureText(str);
    }

    /**
     * 文字绘制X轴居中
     */
    protected float getFontX(String str, Paint p, float centerX) {
        float re = 0;
        re = centerX - p.measureText(str) / 2;
        return re < 0 ? 0 : re;
    }

    protected Drawable setImageBounds(Drawable drawable, float centerX,
                                      float centerY, float w, float h) {

        if (w > 0 && h > 0) {
            drawable.setBounds((int) (centerX - w / 2),
                    (int) (centerY - h / 2), (int) (centerX + w / 2),
                    (int) (centerY + h / 2));
            return drawable;
        }

        float scale = drawable.getIntrinsicWidth() * 1.0f
                / drawable.getIntrinsicHeight();
        if (w > 0) {
            drawable.setBounds((int) (centerX - w / 2), (int) (centerY - w
                    / scale / 2), (int) (centerX + w / 2), (int) (centerY + w
                    / scale / 2));
            return drawable;
        }

        if (h > 0) {
            drawable.setBounds((int) (centerX - h * scale / 2),
                    (int) (centerY - h / 2), (int) (centerX + h * scale / 2),
                    (int) (centerY + h / 2));
            return drawable;
        }

        return drawable;
    }

    protected void drawText(String text, Canvas canvas, Paint paint, float centerX, float centerY) {
        canvas.drawText(text, getFontX(text, paint, centerX), getFontY(centerY, paint), paint);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        // TODO Auto-generated method stub
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
    }

    /**
     * 关闭硬件加速，使view支持阴影
     */
    protected void setSoftwareLayer() {
        if (Build.VERSION.SDK_INT >= 11) {
            setLayerType(LAYER_TYPE_SOFTWARE, null);
        }
    }

}
